using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_KHO
    {
        public clsCHI_TIET_KHO(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private CHI_TIET_KHO GetDataRow(DataRow row)
        {
            CHI_TIET_KHO objCHI_TIET_KHO = new CHI_TIET_KHO();
            
            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objCHI_TIET_KHO.MaKho = (row["MaKho"].ToString().Trim());
            }
            catch { }
           
            try
            {
                if (row["TenKho"] != DBNull.Value)
                    objCHI_TIET_KHO.TenKho = (row["TenKho"].ToString());
            }
            catch { }
            try
            {
                if (row["TenThietBi"] != DBNull.Value)
                    objCHI_TIET_KHO.TenThietBi = (row["TenThietBi"].ToString());
            }
            catch { }
            
            try
            {
                if (row["SoLuongTon"] != DBNull.Value)
                    objCHI_TIET_KHO.SoLuongTon = float.Parse(row["SoLuongTon"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    objCHI_TIET_KHO.MaThietBi = (row["MaThietBi"].ToString().Trim());
            }
            catch { }
            
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_KHO.MaTrangThai = int.Parse(row["MaTrangThai"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_KHO.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }

            try
            {
                if (row["ThietYeu"] != DBNull.Value)
                    objCHI_TIET_KHO.ThietYeu = int.Parse(row["ThietYeu"].ToString().Trim());
            }
            catch { }

            return objCHI_TIET_KHO;
        }
        private List<CHI_TIET_KHO> GetCHI_TIET_KHO(DataTable dt)
        {
            List<CHI_TIET_KHO> lst = new List<CHI_TIET_KHO>();
            CHI_TIET_KHO obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods
        public List<CHI_TIET_KHO> usp_SelectCHI_TIET_KHO(CHI_TIET_KHO obj)
        {
            string spName = "usp_SelectCHI_TIET_KHO";
            DataTable dt = new DataTable();
            List<CHI_TIET_KHO> lst = new List<CHI_TIET_KHO>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaKho", obj.MaKho==""?null:obj.MaKho);
                par[1] = new SqlParameter("@MaThietBi", obj.MaThietBi==""?null:obj.MaThietBi);
                par[2] = new SqlParameter("@MaTrangThai", obj.MaTrangThai);
                par[3] = new SqlParameter("@MaNhomVTPT", obj.MaNhomVTPT==""?null:obj.MaNhomVTPT);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_KHO(dt);
            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }
        #region Public methods

        #endregion Public methods
    }
}
